package com.zygh.lz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zygh.lz.dao.DutyrosterMapper;
import com.zygh.lz.dao.EquipageMapper;
import com.zygh.lz.entity.Dutyroster;
import com.zygh.lz.entity.Equipage;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.entity.StaffEquipage;
import com.zygh.lz.entity.vo.EquipageVO;
import com.zygh.lz.service.StaffEquipageService;
import com.zygh.lz.service.StaffService;
import com.zygh.lz.stapprove.InBean;
import com.zygh.lz.stapprove.OutBeanUtil;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.vo.ResultBeans;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class EquipageServiceImpl extends ServiceImpl<EquipageMapper, Equipage> {

    @Resource
    private EquipageMapper equipageMapper;
    @Resource
    private DutyrosterMapper dutyrosterMapper;

    @Resource
    private StaffService staffService;

    @Resource
    private StaffEquipageService staffEquipageService;

    private static final String EQUIPMENT_WHITE_LIST = "车";

    public ResultBeans getEquipageById(String code) {
        List<Map<String, Object>> equipageById = equipageMapper.getEquipageById(code);
        for (Map<String, Object> stringObjectMap : equipageById) {
            stringObjectMap.put("carNo", stringObjectMap.get("carNo").toString() + stringObjectMap.get("gpsid").toString());
        }
        return ResultUtil.resultBeansSuccess(equipageById);
    }

    public ResultBeans insertEquipage(Equipage equipage) {
        Dutyroster dutyroster = new Dutyroster();
        dutyroster.setGpsid(equipage.getGpsid());
        dutyroster.setWarningsignal(equipage.getWarningSignal());
        dutyroster.setCreatTime(new Date());

        dutyrosterMapper.insertIgnoreNull(dutyroster);
        return ResultUtil.resultBeansSuccess(equipageMapper.insert(equipage));

    }


    public ResultBeans updateEquipage(@RequestBody Equipage equipage) {
        //修改dutyrosterMapper表中设备
        Dutyroster dutyroster = new Dutyroster();
        dutyroster.setGpsid(equipage.getGpsid());
        dutyroster.setWarningsignal(equipage.getWarningSignal());
        dutyrosterMapper.update(dutyroster, Wrappers.<Dutyroster>lambdaQuery().eq(Dutyroster::getGpsid, equipage.getGpsid()));

        return ResultUtil.resultBeansSuccess(equipageMapper.updateEquipage(equipage));
    }


    public List<Map<String, Object>> getByCarNo(String carNo) {
        return equipageMapper.getByCarNo(carNo);
    }

    public ResultBeans policeEquipageBind(Equipage equipage) {
        Equipage one = getOne(Wrappers.<Equipage>lambdaQuery().eq(Equipage::getQrcode, equipage.getQrcode()));
        if (one == null) {
            return ResultUtil.resultBeansError("绑定失败，装备不存在", null);
        }

        // 判断设备类型是否为警车或者摩托车
        if (one.getType().contains(EQUIPMENT_WHITE_LIST)) {
            // 警车和摩托车绑定关系为一对多 不需要判断是否有其他人绑定
            staffEquipageService.saveStaffEquipage(one.getGpsid(), equipage.getWarningSignal());
            // 将警号写入设备信息 返回
            one.setWarningSignal(equipage.getWarningSignal());
            return ResultUtil.resultBeansSuccess(one);
        } else {
            // 一般设备与警员为一对一关系 需要判断是否有其他人绑定
            StaffEquipage staffEquipage = staffEquipageService.lambdaQuery().eq(StaffEquipage::getGpsid, one.getGpsid()).last("limit 1").one();
            if (staffEquipage == null) {
                one.setWarningSignal(equipage.getWarningSignal());
                staffEquipageService.saveStaffEquipage(one.getGpsid(), equipage.getWarningSignal());
                return ResultUtil.resultBeansSuccess(one);
            } else if (staffEquipage.getPoliceNumber().equals(equipage.getWarningSignal())) {
                one.setWarningSignal(equipage.getWarningSignal());
                return ResultUtil.resultBeansError("装备已绑，不需重复绑定", one);
            } else {
                Staff serviceOne = staffService.getOne(Wrappers.<Staff>lambdaQuery().eq(Staff::getStaffNum, staffEquipage.getPoliceNumber()));
                String string = "装备绑定失败，已被" + serviceOne.getStaffName() + "绑定" + "警号为" + serviceOne.getStaffNum();
                return ResultUtil.resultBeansError(string, one);
            }
        }
    }

    public EquipageVO getEquipageVOByQrcode(String qrcode) {
        return equipageMapper.getEquipageVOByQrcode(qrcode);
    }

    /**
     * @author jhoal
     * @description 警员装备未签到绑定30天后自动解绑
     * @date 9:01 2023/5/24
     **/
    public void autoUnbindingEquipment() {
        List<Long> staffEquipageIdList = equipageMapper.getAutoUnbindingEquipageList();
        staffEquipageService.removeByIds(staffEquipageIdList);
    }

    /**
     * @param equipage
     * @return com.zygh.lz.vo.ResultBeans
     * @author jhoal
     * @description 弃用 业务变动方法弃用
     * @date 15:46 2023/4/6
     **/
    public ResultBeans unbindingEquipment(Equipage equipage) {
        Equipage one = getOne(Wrappers.<Equipage>lambdaQuery().eq(Equipage::getGpsid, equipage.getGpsid()).eq(Equipage::getWarningSignal, equipage.getWarningSignal()));
        if (one == null) {
            return ResultUtil.resultBeansError("解绑失败，绑定信息不存在");
        }
        equipageMapper.unbindingEquipment(one.getGpsid());
        return ResultUtil.resultBeansSuccess("解绑成功");
    }
}
